<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色管理</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/easyui/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/easyui/locale/easyui-lang-zh_CN.js"></script>
<style type="text/css">
.input{
	width: 165px;
}
</style>
<script type="text/javascript" language="javascript">
	$(function(){
		
		//自定义验证规则 
		$.extend($.fn.validatebox.defaults.rules,{
			//验证角色名是否存在
			vaildRolename:{ 
				validator:function(value) {
					var res = "" ;
					$.ajax({
						type:'GET',
						async:false,
						data:{
							rolename:value
						},
						url:'${pageContext.request.contextPath }/vaildRoleName',
						success:function(data){
							res = data.data ;
						}
					}) ;
					return res == "true" ;
				},
				message:'此角色已经存在'
			}
		}) ;
		
		
		$("#searchBtn").click(function(){
			var rolename = $("#s_rolename").val() ;
			$("#roleTable").datagrid("load",{
				rolename:rolename
			}) ;
			return false ;
		}) ;
		
		$("#addBtn").click(function(){
			$("#roleid").val("") ;
			$("#rolename").val("") ;
			$("#roledescription").val("") ;
			$("#updateDlg").dialog("open").dialog("setTitle","新增角色") ;
			return false ;
		}) ;
		
		$("#editBtn").click(function(){
			$("#roleid").val("") ;
			$("#rolename").val("") ;
			$("#roledescription").val("") ;
			var rows = $("#roleTable").datagrid("getSelections") ;
			if(rows.length != 1) {
				$.messager.alert("系统提示","请选择一条数据") ;
			} else {
				var row = rows[0] ;
				$("#fm").form("load",row) ;
				$("#rolename").validatebox("disableValidation") ;
				$("#rolename").attr("readonly",true) ;
				$("#updateDlg").dialog("open").dialog("setTitle","更新角色") ;
			}
			return false ;
		}) ;
		
		$("#sqBtn").click(function(){
			var rows = $("#roleTable").datagrid("getSelections") ;
			if(rows.length != 1) {
				$.messager.alert("系统提示","请一个要授权的角色") ;
			} else {
				$("#sqDlg").dialog("open").dialog("setTitle","角色授权") ;
				roleid = rows[0].roleid ;
				$("#sqtree").tree({
					lines:true,
					method:'GET',
					url:'${pageContext.request.contextPath }/sqMenu?parentId=-1&roleId=' + roleid,
					checkbox:true,
					cascadeCheck:false,
					onLoadSuccess:function(){
						$("#sqtree").tree('expandAll') ;
					},
					onCheck:function(node,checked) {
						if(checked) {
							checkNode($("#sqtree").tree("getParent",node.target)) ;
						}
					}
				}) ;
			}
		}) ;
		
		$("#comfireBtn").click(function(){
			//验证表单
			var flag = $("#fm").form("validate") ;
			if(flag) {
				var roleid = $("#roleid").val() ;
				var rolename = $("#rolename").val() ;
				var roledescription = $("#roledescription").val() ;
				$.ajax({
					type:'POST',
					data:{
						roleid:roleid,
						rolename:rolename,
						roledescription:roledescription
					},
					url:'${pageContext.request.contextPath }/updateRole',
					success:function(data){
						$("#updateDlg").dialog("close") ;
						$.messager.alert("系统提示",data.data) ;
						$("#roleTable").datagrid("load") ;
						
					}
					
				}) ;
			}
			return false ;
		}) ;
		
		$("#comfiresqBtn").click(function(){
			var nodes = $("#sqtree").tree("getChecked") ;
			var authArrids = [] ;
			for(var i = 0 ; i < nodes.length ; i++) {
				authArrids.push(nodes[i].id) ;
			}
			var authids = authArrids.join(",") ;
			$.ajax({
				type:'POST',
				data:{
					roleid:roleid,
					authids:authids
				},
				url:'${pageContext.request.contextPath }/sqRole',
				success:function(data){
					$.messager.alert("系统提示",data.data) ;
					$("#sqDlg").dialog("close") ;
				}
			}) ;
			return false ;
		}) ;
		
		$("#removeBtn").click(function(){
			var rows = $("#roleTable").datagrid("getSelections") ;
			if(rows.length == 0) {
				$.messager.alert("系统提示","请选择一条数据") ;
			}else {
				$.messager.confirm("系统提示","您确定要删除这" + rows.length + " 个角色？",function(r){
					if(r) {
						var flag = false ;
						var strIds = [] ;
						var rolenames = "" ; 
						for(var i = 0 ; i < rows.length ; i++) {
							flag = exitsUser(rows[i].roleid) ;
							if(flag) {
								rolenames += rows[i].rolename + "," ;
							}else {
								strIds.push(rows[i].roleid) ;
							}
						}
						if(strIds.length != 0) {
							if(rolenames != "") {
								$.messager.alert("系统提示",rolenames + " 角色存在用户，不能删除") ;								
							}
							var ids = strIds.join(",") ;
							$.ajax({
								type:'DELETE',
								url:'${pageContext.request.contextPath }/removeRole/' + ids,
								success:function(data) {
									$("#roleTable").datagrid("load") ;
									$.messager.alert("系统提示",data.data) ;
								}
							}) ;
						} else {
							$.messager.alert("系统提示",rolenames + " 角色存在用户，不能删除") ;
						}
					}
				}) ;
			}
			
			return false ;
		}) ;
	}) ;
	
	function exitsUser(roleid) {
		var result = "" ;
		$.ajax({
			type:'GET',
			async:false,
			url:'${pageContext.request.contextPath }/existUser?roleid=' + roleid,
			success:function(data) {
				result = data.data ;
			}
		}) ;
		return result == "true" ;
	}
	
	function checkNode(node) {
		if(!node) {
			return ;
		} else {
			checkNode($("#sqtree").tree("getParent",node.target)) ;
			$("#sqtree").tree("check",node.target) ;
		}
	}
</script>
</head>
<body style="margin: 1px;">

	<table id="roleTable" class="easyui-datagrid" data-options="
		title:'角色管理',
		method:'GET',
		url:'${pageContext.request.contextPath }/rolepageList',
		fitColumns:true,
		fit:true,
		pagination:true,
		rownumbers:true,
		toolbar:'#tb'
		">
			<thead>
				<tr>
					<th field="cb" checkbox="true" align="center"></th>
					<th field="roleid" hidden></th>
					<th field="rolename" width="200" align="center">角色名称</th>
					<th field="roledescription" width="300" align="left">备注</th>
				</tr>
			</thead>
	</table>
	
	<div id="tb">
		<div>
			<a id="addBtn" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加角色</a>
			<a id="editBtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改角色</a>
			<a id="removeBtn" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除角色</a>
			<a id="sqBtn" class="easyui-linkbutton" data-options="iconCls:'icon-roleManage',plain:true">角色授权</a>
		</div>
		<hr />
		<div>
			角色名称：<input type="text" id="s_rolename" name="s_rolename"/>
			<a id="searchBtn" class="easyui-linkbutton" style="margin-bottom: 3px;" data-options="iconCls:'icon-search',plain:true">搜索</a>
		</div>
	</div>
	
	<div id="updateDlg" class="easyui-dialog" style="width: 400px;height: 235px;padding: 10px;"
		data-options="modal:true,closed:true,buttons:'#updlg-buttons',iconCls:'icon-edit'">
		<form id="fm" style="margin-top: 5px;margin-left: 50px;">
			<table>
				<tr>
					<td>角色名称：</td>
					<td>
						<input type="hidden" id="roleid" name="roleid" />
						<input class="easyui-validatebox input" id="rolename" name="rolename" required validType="vaildRolename" />
					</td>
				</tr>
				<tr>
					<td align="right">备注：</td>
					<td><textarea rows="3" cols="21" id="roledescription" name="roledescription"></textarea></td>
				</tr>
			</table>
		</form>
	</div>
	<div id="updlg-buttons">
		<a class="easyui-linkbutton" id="comfireBtn" data-options="iconCls:'icon-ok'">确认</a>
	</div>
	
	
	<div id="sqDlg" class="easyui-dialog" style="width: 300px;height: 450px;padding: 10px;"
		data-options="modal:true,closed:true,buttons:'#sqdlg-buttons',iconCls:'icon-edit'">
		<ul id="sqtree" class="easyui-tree">
		</ul>
	</div>
	<div id="sqdlg-buttons">
		<a class="easyui-linkbutton" id="comfiresqBtn" data-options="iconCls:'icon-ok'">确认</a>
	</div>
</body>
</html>